package com.javasm.util;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;

/**
 * @author WangJie
 * @version 1.0
 * @Description:
 * @date: 2020/7/24 22:29
 * @since JDK 1.8
 */
public class MD5Util {

    private MD5Util(){}

    public static String md5(String source){
        Objects.requireNonNull(source);
        String salt = "lisa#&%*_";//一串特殊的内容 盐值
        source=source+salt;
        try {
            //1.创建信息摘要对象
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            //2.将需要解密的文本更新到messageDigest
            messageDigest.update(source.getBytes());
            //3.加密
            byte[] digest = messageDigest.digest();
//            System.out.println(Arrays.toString(digest));
            //将字节数组转换成字符串  BigInteger
            BigInteger bigInteger = new BigInteger(1,digest);
            return bigInteger.toString(16).toUpperCase();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

}

